﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.VikingErik.Mvc.ResumingDownloadsTests
{
    public class ContentRange
    {
        public long StartIndex { get; private set; }
        public long EndIndex { get; private set; }
        public long TotalSize { get; private set; }

        public ContentRange(int startIndex, int endIndex, int totalSize)
        {
            StartIndex = startIndex;
            EndIndex = endIndex;
            TotalSize = totalSize;
        }
    }

    public static class ContentRangeParser
    {
        public static ContentRange ParseRange(string contentRange)
        {
            if (contentRange.IndexOf('-') < 0 &&
                contentRange.IndexOf('/') < 0)
                return null;

            string[] parts = contentRange.Split('-');
            string[] sizes = parts[1].Split('/');
            string[] bytes = parts[0].Split(' ');

            int start, end, total;

            int.TryParse(sizes[1], out total);
            int.TryParse(sizes[0], out end);
            int.TryParse(bytes[1], out start);

            return new ContentRange(start, end, total);
        }
    }
}
